/**
 * 玩家头像
 * by wyy
 */
class player extends GameObject implements eui.UIComponent {
    public coverRect: eui.Rect;
    public headerImg: eui.Image;
    public offlineGrp: eui.Group;
    public timeLbl: eui.Label;
    public nameLbl: eui.Label;
    public resultLbl: eui.Label;
    public playerBorder: eui.Rect;
    public diceGrp1: eui.Group;
    public diceGrp2: eui.Group;

    private result;
    public data;

    constructor(d) {
        super();
        this.skinName = "resource/eui_skins/game_skins/playerSkin.exml";
        this.data = d;
    };
    addToStage() { }
    onRemove() { }
    firstCreate() {
        let id = this.data.id || this.data.uid;
        this.nameLbl.text = this.data.name;
        //显示结果
        this.renewNum(this.data.result);
        //显示时间
        this.renewTime(this.data.roll_time);
        if (this.data.off_line != 1) {
            this.offlineGrp.visible = true;
        }
        else if (this.data.off_line == 1) {
            this.offlineGrp.visible = false;
        }
        //玩家本人指示框
        if (id == globalObject.getInstance().player.Userid) {
            this.playerBorder.visible = true;
        }
        //头像
        let hId = globalObject.getInstance().idToHeadId(id);
        this.headerImg.source = `header${hId}_jpg`;
        this.headerImg.mask = this.coverRect;
    }
    //更新点数
    renewNum(result) {
        if (typeof (result) != "string") {
            result = this.result;
        }
        if (result != "") {
            let s = (<string>result).split(",");
            if (s.length > 2) {
                this.resultLbl.text =
                    `${s[0]}+${s[1]}+${s[2]}+${s[3]}=${Number(s[0]) + Number(s[1]) + Number(s[2]) + Number(s[3])}`;
            }
            else {
                this.resultLbl.text =
                    `${s[0]}+${s[1]}+0+0=${Number(s[0]) + Number(s[1])}`;
            }
        }
        else {
            this.resultLbl.text = "0+0+0+0=0";
        }
    }
    //更新时间
    renewTime(roll_time) {
        if (roll_time <= 0) {
            this.timeLbl.text = "未投掷";
        }
        else {
            let date = new Date(roll_time * 1000);
            let Y = date.getFullYear() + '-';
            let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
            let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            this.timeLbl.text = h + m + s;
        }
    }
    playDice(result) {
        this.result = result;
        if (result != "") {
            this.diceGrp1.removeChildren();
            this.diceGrp2.removeChildren();
            let rArr = result.split(",");
            let dice1Num = rArr[rArr.length - 2];
            let dice2Num = rArr[rArr.length - 1];
            let dice1 = this.createDice(dice1Num);
            let dice2 = this.createDice(dice2Num);
            dice1.scaleX = dice1.scaleY = 0.5;
            dice2.scaleX = dice2.scaleY = 0.5;

            this.diceGrp1.addChild(dice1);
            this.diceGrp2.addChild(dice2);
            dice1.play(2);
            dice2.play(2);
        }
    }

    private createDice(num: number): egret.MovieClip {
        let dice: egret.MovieClip =
            new egret.MovieClip(globalObject.getInstance().diceFactory.generateMovieClipData(num.toString()));
        dice.addEventListener(egret.Event.COMPLETE, this.diceEnd, this);
        return dice;
    }

    private diceEnd(evt) {
        let dice = evt.target;
        setTimeout(() => {
            (dice.parent) && dice.parent.removeChild(dice);
            this.renewNum(this.result);
        }, 300);
    }

}